package com.neusmart.tongrentang.fragment;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.neusmart.tongrentang.R;
import com.neusmart.tongrentang.activity.MainActivity;
import com.neusmart.tongrentang.dataLoadRequest.DataLoadFragment;

/**
 * Created by keven on 2016/8/17.
 */
public class MainFragment extends DataLoadFragment implements View.OnClickListener, RadioGroup.OnCheckedChangeListener{

    private MainActivity mAct;

    private View tabRgContainer;
    private RadioGroup tabRg;
    //
    private Fragment currentFragment;
    private int preciousCheckedId = R.id.main_information;
    //
    private OnReplyLayoutActionListener listener;
    private boolean isReplyLayoutShown;
    //
    private MainInforMationFragment inforMationFragment;//资讯
    private MainSeeDoctorFragment seeDoctorFragment;//看医生
    private MainCommunityFragment communityFragment;//微社区
    private MainMyFragment mainMyFragment;//我的

    public interface OnReplyLayoutActionListener {
        void hideReplyLayout();
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.fragment_main;
    }

    @Override
    protected void init() {
        initView();
        setListener();
    }

    private void initView(){
        tabRgContainer = findViewById(R.id.main_tab_rg_container);
        tabRg = (RadioGroup) findViewById(R.id.main_tab_rg);
        //
        inforMationFragment = new MainInforMationFragment();
        seeDoctorFragment = new MainSeeDoctorFragment();
        communityFragment = new MainCommunityFragment();
        mainMyFragment = new MainMyFragment();
        //
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        transaction.add(R.id.main_content_frame, inforMationFragment).commit();
        currentFragment = inforMationFragment;
    }

    private void setListener(){
        int[] resIds = {};
        for (int id : resIds){
            findViewById(id).setOnClickListener(this);
        }
        tabRg.setOnCheckedChangeListener(this);
    }

    @Override
    public void onClick(View v) {}

    /*@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
                case RequestCode.LOGIN_COMMUNITY:
                    switchContent(communityFragment);
                    break;
                case RequestCode.LOGIN_DATING_CAR:
                    switchContent(datingCarFragment);
                    break;
                default:
                    break;
            }
        } else {
            ((RadioButton) mContentView.findViewById(preciousCheckedId)).setChecked(true);
        }
    }*/

    public boolean isCurrentIsNotIndex() {
        return preciousCheckedId != R.id.main_information;
    }

    public void hideReplyLayout() {
        if (listener != null)
            listener.hideReplyLayout();
    }

    public boolean isReplyLayoutShown() {
        return isReplyLayoutShown;
    }

    public void switch2Index() {
        ((RadioButton) mContentView.findViewById(R.id.main_information)).setChecked(true);
    }

    private void switchContent(Fragment to) {
        if (currentFragment == null || to == null || currentFragment == to)
            return;
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        if (!to.isAdded())
            transaction.hide(currentFragment).add(R.id.main_content_frame, to).commitAllowingStateLoss();
        else
            transaction.hide(currentFragment).show(to).commitAllowingStateLoss();
        currentFragment = to;
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId){
            case R.id.main_information://资讯
                switchContent(inforMationFragment);
                break;
            case R.id.main_look_doctor://看医生
                switchContent(seeDoctorFragment);
                break;
            case R.id.main_club://微社区
                switchContent(communityFragment);
                break;
            case R.id.main_my://我的
                switchContent(mainMyFragment);
                break;
            default:
                break;
        }
        preciousCheckedId = checkedId;
    }

}
